<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox
            v-model="formData.remember"
            @click="formData.remember = !formData.remember"
            >记住我</el-checkbox
          >
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="doLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Message } from "element-ui";
import { mapActions } from "vuex";

const FORMDATA_KEY = "form_key";
export default {
  name: "Login",

  //第一步,实现表单基本校验
  data() {
    //初始化表单数据
    return {
      formData: {
        username: "",
        password: "",
        remember: false,
      },
      //制定校验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  computed: {},
  //在组件初始化的时候检查有没有本地的表单数据
  created() {
    const localInfo = localStorage.getItem(FORMDATA_KEY);
    if (localInfo) {
      const { username, password } = JSON.parse(localInfo);

      this.formData = { username, password, remember: true };
    }
  },

  methods: {
    ...mapActions("user", ["loginAction"]),
    //第二步点击登录统一校验,校验通过发送登录请求保存token到vuex中
    //1.表单校验
    //2.调用登录接口
    doLogin() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const { username, password } = this.formData;
          console.log(username, password);
          try {
            await this.loginAction({ username, password });
             Message.success("登录成功");
              this.$router.push("/");
            //如果选择了记住我,保存到本地存储中
            if (this.formData.remember) {
              localStorage.setItem(FORMDATA_KEY, JSON.stringify(this.formData));
            } else {
              localStorage.removeItem(FORMDATA_KEY);
            }
          } catch (err) {
            Message.error(err.message);
          }
        } else {
          Message.error("请输入用户名或密码");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
}
.bg {
  width: 60vw;
  height: 100vh;
  background: url("~@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}
.box {
  margin: 200px 10% 0;
  flex: 1;
  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }
    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }
    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}
.login_btn {
  width: 100%;
}
</style>
